package com.duobaoyu.dby.supply.api.controller;

import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.security.annotation.Inner;
import com.duobaoyu.dby.supply.api.service.OrderBizService;
import com.duobaoyu.dby.supply.common.biz.remote.dto.order.*;
import com.duobaoyu.dby.supply.common.biz.remote.to.order.CancelOrderTO;
import com.duobaoyu.dby.supply.common.biz.remote.to.order.OrderDetailTO;
import com.duobaoyu.dby.supply.common.biz.remote.to.order.OrderTO;
import com.pig4cloud.plugin.idempotent.annotation.Idempotent;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 订单
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/9/26 10:14
 */
@Inner
@Validated
@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
public class OrderController {

	private final OrderBizService orderBizService;

	/**
	 * 提交订单
	 */
	@Idempotent(delKey = true)
	@ApiOperation("提交订单")
	@PostMapping("/submit")
	public R<OrderTO> submitOrder(@Valid @RequestBody SubmitOrderDTO submitOrder) {
		OrderTO order = orderBizService.submitOrder(submitOrder);
		return Optional.ofNullable(order).map(OrderTO::getSuccess).orElse(Boolean.FALSE) ? R.ok(order) : R.failed(order);
	}

	/**
	 * 确认订单
	 */
	@Idempotent(delKey = true)
	@ApiOperation("确认订单")
	@PostMapping("/confirm")
	public R<Boolean> confirmOrder(@Valid @RequestBody ConfirmOrderDTO confirmOrder) {
		return R.ok(orderBizService.confirmOrder(confirmOrder));
	}

	/**
	 * 确认收货
	 */
	@Idempotent(delKey = true)
	@ApiOperation("确认收货")
	@PostMapping("/confirm-receive")
	public R<Boolean> confirmReceive(@Valid @RequestBody ConfirmReceiveDTO confirmReceive) {
		return R.ok(orderBizService.confirmReceive(confirmReceive));
	}


	/**
	 * 取消订单
	 */
	@Idempotent(delKey = true)
	@ApiOperation("取消订单")
	@PostMapping("/cancel")
	public R<List<CancelOrderTO>> cancelOrder(@Valid @RequestBody CancelOrderDTO cancelOrder) {
		return R.ok(orderBizService.cancelOrder(cancelOrder));
	}


	/**
	 * 查询订单
	 */
	@ApiOperation("查询订单")
	@GetMapping("/query")
	public R<List<OrderDetailTO>> queryOrder(OrderQueryDTO orderQuery) {
		return R.ok(orderBizService.queryOrder(orderQuery));
	}

}
